package cn.hefan.web.servlet.phone;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hefan.domain.Phone;
import cn.hefan.domain.ResultInfo;
import cn.hefan.service.PhoneService;

public class addPhoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				//解决中午乱码问题
				request.setCharacterEncoding("UTF-8");
				
				Map<String, String[]> map = request.getParameterMap();
				//创建user对象
				Phone phone = new Phone();
				try {
					BeanUtils.populate(phone, map);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ResultInfo info = null;
				if(phone.getName()!=null&&phone.getPhone()!=null&&phone.getPro()!=null&&phone.getTri()!=null) {
					info = PhoneService.addPhone(phone);
				}
				//响应数据
				ObjectMapper mapper = new ObjectMapper();
				response.setContentType("application/json;charset=utf-8");
				mapper.writeValue(response.getOutputStream(),info);
				
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

}